using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using MyApi.Entity;
using MyApi.Data;
namespace MyApi.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        //初始化一个数据库上下文变量
        private EntityDb db;
        //构造函数
        public EfRepository(EntityDb dbs)
        {
            db = dbs;
        }

        private DbSet<T> _entity;

        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = db.Set<T>();
                }
                return _entity;
            }
        }

        //查询表
        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            var t= Entity.Where(x=>x.Id==id).FirstOrDefault();
            Entity.Remove(t);
            db.SaveChanges();
        }
        //查找指定实体
        public T GetById(int id)
        {
            var t=Entity.Where(x=>x.Id==id).FirstOrDefault();
            return t;
        }
        //插入数据
        public void Insert(T entity)
        {
            if(_entity==null){
                throw new System.NullReferenceException();
            }
            entity.IsActived=true;
            entity.IsDeleted=false;
            entity.CreatedTime=DateTime.Now;
            entity.UpdatedTime=DateTime.Now;
            entity.DisplayOrder=0;
            entity.Remarks="";

            Entity.Add(entity);
            db.SaveChanges();
        }

        //异步插入
        public async Task InsertAsync(T entity)
        {
            if(entity==null){
                throw new System.NullReferenceException();
            }
            entity.IsActived=true;
            entity.IsDeleted=false;
            entity.CreatedTime=DateTime.Now;
            entity.UpdatedTime=DateTime.Now;
            entity.DisplayOrder=0;
            entity.Remarks="";
            await Entity.AddAsync(entity);
            await db.SaveChangesAsync();
        }

        //批量插入
        public void InsertBulk(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.IsActived=true;
                entity.IsDeleted=false;
                entity.CreatedTime=DateTime.Now;
                entity.UpdatedTime=DateTime.Now;
                entity.DisplayOrder=0;
                entity.Remarks="";
            }
            Entity.AddRange(entities);
            db.SaveChanges();
        }

        //异步批量插入
        public async Task InsertBulkAsync(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.IsActived=true;
                entity.IsDeleted=false;
                entity.CreatedTime=DateTime.Now;
                entity.UpdatedTime=DateTime.Now;
                entity.DisplayOrder=0;
                entity.Remarks="";
            }
            await Entity.AddRangeAsync(entities);
            await db.SaveChangesAsync();
        }

        //修改
        public void Update(T entity)
        {
            if(entity==null){
                throw new System.NullReferenceException();
            }
            entity.UpdatedTime=DateTime.Now;
            db.SaveChanges();
        }

        //批量修改
        public void UpdateBulk(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.UpdatedTime=DateTime.Now;
            }
            db.SaveChanges();
        }
    }
}